package web.error;

import framework.config.ErrorConfig;
import framework.exceptions.BusinessException;
import framework.exceptions.NotFoundException;
import framework.exceptions.RequestException;
import framework.exceptions.WebException;
import framework.runtime.JsonModelView;
import framework.utils.RequestUtil;
import framework.vo.ResultInfo;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;

class SysErrorHandler {

    public static ModelAndView handleWebException(int status, int code, String message, HttpServletResponse response) throws IOException {
        ModelAndView view = new ModelAndView();
        view.setStatus(HttpStatus.valueOf(status));
        //
        view.addObject("status", status);
        view.addObject("code", code);
        view.addObject("message", message);
        //
        if (status == 500)
            view.setViewName("error/500");
        else if (status == 400)
            view.setViewName("error/400");
        else if (status == 401)
            view.setViewName("error/401");
        else if (status == 403)
            view.setViewName("error/403");
        else if (status == 404)
            view.setViewName("error/404");
        else if (status == 405)
            view.setViewName("error/405");
        else if (status == 200)
            view.setViewName("error/200");
        return view;
    }

    /**
     * 获得异常视图
     *
     * @param e
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    public static ModelAndView handleException(ErrorConfig errorConfig, Throwable e, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ModelAndView view = null;
        if (errorConfig.getOutputJson() || RequestUtil.isAjaxRequest(request)) {
            ResultInfo resultInfo = new ResultInfo();
            resultInfo.setMsg(e.getMessage());
            if (e instanceof WebException) {
                response.setStatus(((WebException) e).errorCode());
                resultInfo.setCode(((WebException) e).errorCode());
            } else if (e instanceof BusinessException) {
                response.setStatus(200);
                resultInfo.setCode(((BusinessException) e).getCode());
            } else if (e instanceof FileNotFoundException) {
                resultInfo.setCode(404);
                response.setStatus(404);
            } else if (e instanceof NotFoundException) {
                resultInfo.setCode(404);
                response.setStatus(404);
            } else if (e instanceof RequestException) {
                view = handleWebException(400, 400, e.getMessage(), response);
                resultInfo.setCode(400);
                response.setStatus(400);
            } else {
                resultInfo.setCode(500);
                response.setStatus(500);
            }
            return new JsonModelView(resultInfo);
        } else {
            if (e instanceof WebException) {
                view = handleWebException(((WebException) e).errorCode(), ((WebException) e).errorCode(), e.getMessage(), response);
            } else if (e instanceof BusinessException) {
                view = handleWebException(200, ((BusinessException) e).getCode(), e.getMessage(), response);
            } else if (e instanceof FileNotFoundException) {
                view = handleWebException(404, 404, e.getMessage(), response);
            } else if (e instanceof NotFoundException) {
                view = handleWebException(404, 404, e.getMessage(), response);
            } else if (e instanceof RequestException) {
                view = handleWebException(400, 400, e.getMessage(), response);
            } else {
                view = handleWebException(500, 500, e.getMessage(), response);
            }
        }
        return view;
    }

}
